package org.andao.commons.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.andao.core.base.model.RootModel;
import org.andao.core.config.Constants;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Where;


/**
 * 系统消息.<p>
 * @author Marco.hu(hzg139@163.com)
 *
 */
@Entity
@Table(name="seewo_sys_msg")  
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Message extends RootModel{
	
	private static final long serialVersionUID = -6399621128605989289L;
	
	@Column(name="MSGTITLE",length=255,nullable=false)	
	private String msgTitle;//消息标题
	
	@Column(name="MSGTYPE",nullable=false)
	private String msgType;//消息类型 取自字典 tips - 温馨提示 / sysmsg - 系统消息/ usermsg - 用户消息
	
	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(name = "CONTENT", nullable = false)	
	private String content;//消息内容
	
	@Column(name="SENDTIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date sendTime;//发送时间
	
	@Column(name="TOTIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date toTime;//倒计时时间
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENTID")
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
	private Message parent;//父
	
	@Column(name="SENDTYPE")
	private String sendType;//附加发送选项 portal - 发送到门户 / email - 发送到邮件 / sms - 发送到手机
	
	@Column(name="ISDRAFT")
	private String isDraft = Constants.BOOLEAN_NO;//是否草稿
	
	@Column(name="ISREPLY")
	private String isReply = Constants.BOOLEAN_NO;//是否允许回复,非用户消息都不能回复
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
	@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL})
	@Where(clause="isDeleted=0")
	@OrderBy("sendTime DESC")
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	private Set<Message> childs = new HashSet<Message>(0);

	@Column(name="FORMID")
	private String formId;//表单ID，消息与业务关联的表单ID
	
	@Column(name="FORMTYPE")
	private String formType;//表单类型，如bbsreply,exicerreply等等，查询时用来缩小范围
	
	@Transient
	private List<Attachs> attachs = new ArrayList<Attachs>();
		
	@Transient
	private boolean readed;//是否已读	
			
	
	/**
	 * @return the formId
	 */
	public String getFormId() {
		return formId;
	}

	/**
	 * @param formId the formId to set
	 */
	public void setFormId(String formId) {
		this.formId = formId;
	}

	/**
	 * @return the formType
	 */
	public String getFormType() {
		return formType;
	}

	/**
	 * @param formType the formType to set
	 */
	public void setFormType(String formType) {
		this.formType = formType;
	}

	public String getIsReply() {
		return isReply;
	}

	public void setIsReply(String isReply) {
		this.isReply = isReply;
	}

	public String getIsDraft() {
		return isDraft;
	}

	public void setIsDraft(String isDraft) {
		this.isDraft = isDraft;
	}

	public List<Attachs> getAttachs() {
		return attachs;
	}

	public void setAttachs(List<Attachs> attachs) {
		this.attachs = attachs;
	}

	public Date getToTime() {
		return toTime;
	}

	public void setToTime(Date toTime) {
		this.toTime = toTime;
	}

	public Set<Message> getChilds() {
		return childs;
	}

	public void setChilds(Set<Message> childs) {
		this.childs = childs;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMsgTitle() {
		return msgTitle;
	}

	public void setMsgTitle(String msgTitle) {
		this.msgTitle = msgTitle;
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	public Message getParent() {
		return parent;
	}

	public void setParent(Message parent) {
		this.parent = parent;
	}

	

	public Date getSendTime() {
		return sendTime;
	}

	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}

	public String getSendType() {
		return sendType;
	}

	public void setSendType(String sendType) {
		this.sendType = sendType;
	}

	public boolean isReaded() {
		return readed;
	}

	public void setReaded(boolean readed) {
		this.readed = readed;
	}

	
	
}
